﻿
using CmdPrep.Algorithms;
using CmdPrep.Backends;
using CmdPrep.Frontends;
using CmdPrep.Properties;
using System;
using System.Data;
using System.Windows.Forms;


namespace CmdPrep
{
    static class Program
    {
        #region Private static data
        
        private static MenuForm             menuForm_;
        private static ProgramSettingsForm  programSettingsForm_;
        
        #endregion
        
        #region Private static methods
        
        private static bool buildModules()
        {            
            menuForm_         = new MenuForm();
            
            return true;           
        }
        
        private static bool connectModules()
        {
            menuForm_.Load += (sender, arg) =>
            {
                Properties.ProgramSettings settings = Properties.ProgramSettings.Default;
                DataTable projectsDataTable         = DbfBackend.getProjectsDataTable(settings.projectsFileInfo);
                menuForm_.setProjectsDataTable(projectsDataTable);
            };
            
            Properties.ProgramSettings.Default.projectSettingsChanged += () =>
            {
                Properties.ProgramSettings settings = Properties.ProgramSettings.Default;
                DataTable projectsDataTable         = DbfBackend.getProjectsDataTable(settings.projectsFileInfo);
                menuForm_.setProjectsDataTable(projectsDataTable);
            };
            
            menuForm_.editProgramSettingsRequested += () =>
            {
                if (programSettingsForm_ == null)
                    programSettingsForm_ = new ProgramSettingsForm();
                
                Properties.ProgramSettings settings = Properties.ProgramSettings.Default;
                programSettingsForm_.editProgramSettings(settings);
            };
            
            menuForm_.cmdFileListRequested += (projectName) =>
            {
//                string projectsContentDir = ProgramSettings.Default.projectsContentDirectoryInfo.FullName;
                string projectsContentDir = ProgramSettings.Default.rootDirectoryInfo.FullName;
                menuForm_.setProjectFilesDataTable( CmdBackend.getCmdDataTable(projectsContentDir, projectName) );
            };
            
            menuForm_.sdlFileListRequested += (projectName) =>
            {
// Same shit as above!!!
                string projectsContentDir = ProgramSettings.Default.rootDirectoryInfo.FullName;
                menuForm_.setProjectFilesDataTable( SdlBackend.getSdlDataTable(projectsContentDir, projectName) );
            };
            
            menuForm_.inputCmdFileRequested += (hintDirName) => 
            {
                string projectsContentDirectory = ProgramSettings.Default.projectsContentDirectoryInfo.FullName;
                string inputCmdFullName = CmdBackend.getInputCmdFileFromUser(projectsContentDirectory, hintDirName);
                
                if (! String.IsNullOrEmpty(inputCmdFullName))
                    menuForm_.addInputCmdFileIfExclusive(inputCmdFullName);
            };
            
            menuForm_.outputCmdFileRequested += (hintFileName) => 
            {
                string projectsContentDirectory = ProgramSettings.Default.projectsContentDirectoryInfo.FullName;
                string outputCmdFullName = CmdBackend.getOutputCmdFileFromUser(projectsContentDirectory, hintFileName);
                
                if (! String.IsNullOrEmpty(outputCmdFullName))
                    menuForm_.addOutputCmdFile(outputCmdFullName);
            };
            
            menuForm_.processCmdFileRequested += (inputCmdFullName, outputCmdFullName, keepEqualSign, skipEmptyLines) => 
            {
                if (CmdProcessing.processCmdFile(inputCmdFullName, outputCmdFullName, keepEqualSign, skipEmptyLines))
                    menuForm_.addOutputCmdFile(outputCmdFullName);
            };
            
            menuForm_.editCmdFileRequested += (fullName) =>
            {
                CmdProcessing.editCmdFile(fullName);
            };
            
            menuForm_.diffCmdFilesRequested += (fullName1, fullName2) =>
            {
                CmdProcessing.diffCmdFiles(fullName1, fullName2);
            };
            
            menuForm_.deleteCmdFileRequested += (fullName) =>
            {
                CmdBackend.deleteCmdFile(fullName);
            };
            
            menuForm_.exitRequested += () =>
            {
                Application.Exit();
            }; 

            return true;            
        }
        
        #endregion
        
        #region Main

        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
            bool applicationIsInitialized = buildModules() && connectModules();
            
            if (applicationIsInitialized)            
                Application.Run(menuForm_);
        }

        #endregion
    }
}
